package com.tarena.lbs.basic.web.service.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.api.PicUpdateParam;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.repository.BusinessRepository;
import com.tarena.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.common.basic.enums.BusStatusEnum;
import com.tarena.lbs.pojo.basic.bo.BusinessBO;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private AttachApi attachApi;
    @Override
    public BasePageList<BusinessBO> pageList(BusinessQuery businessQuery) {
        return businessRepository.getBusinessListByQuery(businessQuery);
    }

    //新增商家信息
    @Override
    public void businessSave(BusinessParam businessParam) {
        log.debug("新增商家参数 BusinessParam:{}",businessParam);
        BusinessBO businessBO = new BusinessBO();
        BeanUtils.copyProperties(businessParam,businessBO);
        //设置入职时间
        businessBO.setEntryTime(new Date());
        //设置审核状态
        businessBO.setBusinessStatus(BusStatusEnum.APPROVED.getCode());
        log.debug("新增商家参数 businessBO:{}",businessBO);
        //调用仓储层写入
        Integer busId = businessRepository.save(businessBO);
        log.debug("保存数据出参 busId:{}",busId);
        //2.调用绑定图片
        businessBO.setId(busId);
        bindPictures(businessBO);
    }

    @Override
    public boolean existsBusiness(Integer businessId) {
        log.debug("判断商家是否存在 businessId:{}",businessId);
        Long count = businessRepository.countBusinessById(businessId);
        return count>0;
    }

    private void bindPictures(BusinessBO businessBO) {
        log.debug("调用attachAPI 处理图片信息入参:{}",businessBO);
        //1.远程调用AttachApi接口方法参数
        List<PicUpdateParam> attchParams = new ArrayList<>();
        String license = businessBO.getBusinessLicense();
        String logo = businessBO.getBusinessLogo();
        //封装Attach参数
        packageAttach(businessBO, license, logo, attchParams);
        log.debug("绑定图片入参:{}",attchParams);
        try{
            boolean result = attachApi.batchUpdateBusiness(attchParams);
            if (!result){
                log.error("新增图片失败,检查对方接口日志");
            }
        }catch (Exception e){
            log.error("建立远程调用图片失败",e);
        }

    }

    //封装Attach Info
    private void packageAttach(BusinessBO businessBO, String license, String logo, List<PicUpdateParam> attchParams) {
        log.debug("分装照片的入参: BusinessBO:{}",businessBO);
        //1.1营业执照封装参数
        PicUpdateParam paramLicense = new PicUpdateParam();
        paramLicense.setBusinessId(businessBO.getId());
        paramLicense.setBusinessType(100);
        paramLicense.setFileUuid(endString(license));
        //1.2logo 图片封装
        PicUpdateParam paramlogo=new PicUpdateParam();
        paramlogo.setBusinessId(businessBO.getId());
        paramlogo.setBusinessType(100);
        paramlogo.setFileUuid(endString(logo));
        attchParams.add(paramLicense);
        attchParams.add(paramlogo);
    }

    //切割字符串,获取最后一个
    private String endString(String str){
        String[] parts = str.split("/");
        return parts[parts.length - 1];
    }

}
